package com.tools.module.base.web;

import com.tools.common.config.AbstractController;
import com.tools.common.model.Result;
import com.tools.common.util.DateUtils;
import com.tools.common.util.ShiroUtils;
import com.tools.module.base.entity.BaseResource;
import com.tools.module.base.service.BaseResourceService;
import com.tools.module.base.service.impl.UploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/base/resource")
public class BaseResourceController extends AbstractController {

    @Autowired
    private BaseResourceService resourceService;
    @Autowired
    private UploadFileService uploadFileService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public Result list(BaseResource resource) {
        return resourceService.list(resource);
    }

    /**
     * 查询
     */
    @PostMapping("/get")
    public Result get(Long id) {
        return resourceService.get(id);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody BaseResource resource) {
        if (resource.getId()==null){
            resource.setCreateTime(DateUtils.getTimestamp());
            resource.setCreateUid(ShiroUtils.getUserId());
        }
        resource.setUpdateUid(ShiroUtils.getUserId());
        Long aLong = resourceService.queryBySeq(resource);
        if (aLong>0) {
            resourceService.updateSeq(resource);
        };
        return resourceService.save(resource);
    }

    @PostMapping("/upload")
    public Result save1(@RequestParam("file") MultipartFile file) {
        Result result = null;
        try {
            result = uploadFileService.fileUpload(file);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error((String) result.get("msg"));
        }
        return result;
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(Long id) {
        return resourceService.delete(id);
    }

}
